package junior.db.dao.enums;

/**
 * @author wubo
 * @date 2024/7/26 20:30
 */
public enum Operator {
    EQ(" = ", false),
    NEQ(" <> ", false),
    GT(" > ", false),
    LT(" < ", false),
    GTE(" >= ", false),
    LTE(" <= ", false),
    BT(" between ", false),
    NULL(" is null", false),
    NOT_NULL(" is not null", false),
    IN(" in ", false),
    NOT_IN(" not in ", false),
    LIKE(" like ", false),
    NOT_LIKE(" not like ", false),
    OR(" or ", true),
    AND(" and ", true),
    LEFT_BRACKET(" ( ", true),
    RIGHT_BRACKET(" ) ", true);
    
    private String optString;
    private boolean combinator;
    
    private Operator(String optString, boolean combinator) {
        this.optString = optString;
        this.combinator = combinator;
    }
    
    public String getOptString() {
        return optString;
    }
    
    public boolean isCombinator() {
        return combinator;
    }
    
    public static Operator fromString(String opt) {
        if("eq".equalsIgnoreCase(opt)) {
            return EQ;
        }
        if("neq".equalsIgnoreCase(opt)) {
            return NEQ;
        }
        if("gt".equalsIgnoreCase(opt)) {
            return GT;
        }
        if("lt".equalsIgnoreCase(opt)) {
            return LT;
        }
        if("gte".equalsIgnoreCase(opt)) {
            return GTE;
        }
        if("lte".equalsIgnoreCase(opt)) {
            return LTE;
        }
        if("bt".equalsIgnoreCase(opt)) {
            return BT;
        }
        if("null".equalsIgnoreCase(opt)) {
            return NULL;
        }
        if("notnull".equalsIgnoreCase(opt)) {
            return NOT_NULL;
        }
        if("in".equalsIgnoreCase(opt)) {
            return IN;
        }
        if("notin".equalsIgnoreCase(opt)) {
            return IN;
        }
        if("like".equalsIgnoreCase(opt)) {
            return LIKE;
        }
        if("notlike".equalsIgnoreCase(opt)) {
            return NOT_LIKE;
        }
        if("or".equalsIgnoreCase(opt)) {
            return OR;
        }
        if("and".equalsIgnoreCase(opt)) {
            return AND;
        }
        if("leftbracket".equalsIgnoreCase(opt)) {
            return LEFT_BRACKET;
        }
        if("rightbracket".equalsIgnoreCase(opt)) {
            return RIGHT_BRACKET;
        }
        throw new RuntimeException("Unknown operator : "+opt);
    }
}
